package com.biz.aoao.mysql.mysqlbinlogsync.test;

import com.alibaba.fastjson.JSON;
import com.github.shyiko.mysql.binlog.BinaryLogClient;
import com.github.shyiko.mysql.binlog.event.*;
import com.github.shyiko.mysql.binlog.event.deserialization.EventDeserializer;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class App {

    public static void main( String[] args ) throws IOException
    {
        BinaryLogClient client = new BinaryLogClient("47.105.110.244", 3306, "root", "Falsesoul1207");

        client.setBinlogFilename("mysql-bin.000001");
        client.setBinlogPosition(120);  //从指定的位置读取binlog，具体位置可以在mysql中查
//        EventDeserializer eventDeserializer = new EventDeserializer();
//        eventDeserializer.setCompatibilityMode(
//                EventDeserializer.CompatibilityMode.DATE_AND_TIME_AS_LONG,
//                EventDeserializer.CompatibilityMode.CHAR_AND_BINARY_AS_BYTE_ARRAY
//        );
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        client.setEventDeserializer(eventDeserializer);

        client.registerEventListener(new BinaryLogClient.EventListener() {

            @Override
            public void onEvent(Event event) {
                EventData data = event.getData();
                EventHeader header = event.getHeader();

                Date date=new Date( header.getTimestamp());

                System.out.println("header["+sdf.format(date)+"]  "+JSON.toJSON(header));
                System.out.println("data  "+JSON.toJSON(data));

                System.out.println();
                System.out.println();

//
//                if (data instanceof UpdateRowsEventData) {
//                    System.out.println("Update--------------");
//                    System.out.println(data.toString());
//                } else if (data instanceof WriteRowsEventData) {
//                    System.out.println("Write---------------");
//                    System.out.println(data.toString());
//                } else if (data instanceof DeleteRowsEventData) {
//                    System.out.println("Delete--------------");
//                    System.out.println(data.toString());
//                }
            }
        });
        client.connect();
    }
}
